// Package models
// @Title  articleTagsModel.go
// @Description  文章标签模型
// @Author  小斌 (时间 格式是2021/12/02  10:20)
// @Update  小斌 (时间 格式是2021/12/02  10:20)

package models

import (
	"github.com/astaxie/beego/orm"
	"time"
)

// ArticleTagsObj 文章目录 实体类
type ArticleTagsObj struct {
	Id                 int    `valid:"id"`
	TagsName                string `valid:"tags_name"`
	Status				int `valid:"status"`
	IsDelete				int `valid:"is_delete"`
	UpdateTime                time.Time `valid:"update_time" orm:"auto_now"`
	AddTime                time.Time `valid:"add_time" orm:"auto_now_add"`
}

func (this *ArticleTagsObj) TableName() string {
	return "article_tags"
}

func init() {
	orm.RegisterModel(new(ArticleTagsObj))
}

// GetArticleTagsList 获取分页数据
func GetArticleTagsList(pageSize int, page int) (*[]ArticleTagsObj, int, error) {
	var (
		list []ArticleTagsObj
		err  error
	)
	qs := orm.NewOrm().QueryTable(new(ArticleTagsObj)).OrderBy("-add_time")

	_, err = qs.Filter("is_delete",0).Limit(pageSize).Offset((page - 1) * pageSize).RelatedSel().All(&list)
	count, _ := qs.Filter("is_delete",0).Count()

	if err == orm.ErrNoRows || err == orm.ErrMissPK {
		return nil, 0, err
	}

	return &list, int(count), nil
}

// GetArticleTagsInfoById 根据ID获取记录
func GetArticleTagsInfoById(id int) (ArticleTagsObj,error) {
	var (
		row ArticleTagsObj
		err  error
	)

	err = orm.NewOrm().QueryTable(new(ArticleTagsObj)).Filter("id", id).Filter("is_delete", 0).One(&row)
	if err == orm.ErrNoRows || err == orm.ErrMissPK {
		return row,nil
	}

	return row,nil
}

// GetArticleTagsListByIds 根据多个id获取记录
func GetArticleTagsListByIds(idArr []int) (*[]ArticleTagsObj,error) {
	var (
		list []ArticleTagsObj
		err  error
	)
	qs := orm.NewOrm().QueryTable(new(ArticleTagsObj)).OrderBy("-add_time")

	_,err = qs.Filter("id__in", idArr).Filter("is_delete", 0).All(&list)

	if err == orm.ErrNoRows || err == orm.ErrMissPK {
		return &list,nil
	}

	return &list,nil
}

// GetUseArticleTagsList 获取启用的文章目录
func GetUseArticleTagsList() (*[]ArticleTagsObj,error) {
	var (
		list []ArticleTagsObj
		err  error
	)
	qs := orm.NewOrm().QueryTable(new(ArticleTagsObj)).OrderBy("-add_time")

	_, err = qs.Filter("status",0).Filter("is_delete",0).Limit(10).Offset(0).RelatedSel().All(&list)
	if err == orm.ErrNoRows || err == orm.ErrMissPK {
		return nil, err
	}

	return &list, nil
}

// AddArticleTags 添加文章目录
func AddArticleTags(tagsName string,status int) int64 {
	articleTags := ArticleTagsObj{
		TagsName:    tagsName,
		Status: status,
		IsDelete: 0,
	}

	id,err := orm.NewOrm().Insert(&articleTags)

	if err == orm.ErrNoRows || err == orm.ErrMissPK {
		return 0
	}

	return id
}

// UpdateArticleTagsById 修改文章目录
func UpdateArticleTagsById(id int,tagsName string,status int) int64 {
	articleTags := ArticleTagsObj{
		Id:    id,
		TagsName:    tagsName,
		Status: status,
	}
	num,err := orm.NewOrm().Update(&articleTags,"tags_name","status")

	if err == orm.ErrNoRows || err == orm.ErrMissPK {
		return 0
	}

	return num
}

// DeleteArticleTagsById 删除文章目录
func DeleteArticleTagsById(id int) int64 {
	articleTags := ArticleTagsObj{
		Id:    id,
		IsDelete: 1,
	}
	num,err := orm.NewOrm().Update(&articleTags,"is_delete")
	if err == orm.ErrNoRows || err == orm.ErrMissPK {
		return 0
	}

	return num
}